using System;
using System.Linq;
using CommandSystem;

namespace RgCore.Modules.Commands.SubPluginManagerCommands
{
    public class Reload<T> : ICommand where T : Attribute, IName
    {
        public Reload(string commandHead, SubPluginManager<T> manager)
        {
            CommandHead = commandHead;
            Manager = manager;
        }

        public string CommandHead { get; }
        public SubPluginManager<T> Manager { get; private set; }

        public string GetUsage() => $"用法: {Command} reload <类名>";

        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.ServerConsoleCommands))
            {
                response = "你没有权限执行此命令";
                return false;
            }

            if (arguments.Count < 1)
            {
                response = $"用法 {Command} reload <类名>";
                return false;
            }

            if (Manager.AllTypes.Any(p => p.Key.Name == arguments.At(0)))
            {
                Manager.ReloadObject(arguments.At(0));
                response = "已重新加载对应项目";
                return true;
            }

            response = "未找到对应的项目！";
            return false;
        }

        public string Command { get; } = "reload";
        public string[] Aliases { get; } = Array.Empty<string>();
        public string Description { get; } = "获取已加载的子项目列表";
    }
}